﻿using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using ZR.Admin.WebApi.Filters;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Model.System;
using ZR.Service.IService.ProductionControl;

namespace ZR.Admin.WebApi.Controllers
{
    /// <summary>
    /// 加工记录表
    /// </summary>
    [Verify]
    [Route("business/CksSpotCheck")]
    [ApiExplorerSettings(GroupName = "business")]
    public class CksSpotCheckController : BaseController
    {
        private readonly ICksSpotCheckService _cksSpotCheckService;
        public CksSpotCheckController(ICksSpotCheckService cksSpotCheckService) {
            _cksSpotCheckService = cksSpotCheckService;
        }

        /// <summary>
        /// 查询抽检列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("list")]
        [AllowAnonymous]
        public IActionResult QueryCksSpotCheck([FromQuery] CksSpotCheckQueryDto parm) {
            var response = _cksSpotCheckService.GetList(parm);
            return SUCCESS(response);
        }

        /// <summary>
        /// 修改抽检记录
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [Log(Title = "产品抽检", BusinessType = BusinessType.UPDATE)]
        [HttpPut]
        [ActionPermissionFilter(Permission = "produce:cksspotcheck:edit")]
        public IActionResult UpdateCksSpotCheck([FromBody] CksSpotCheckDto parm) {
            if (parm == null || parm.SpotCheckId <= 0) {
                return ToResponse(ResultCode.PARAM_ERROR,"params error.");
            }
            int result = _cksSpotCheckService.UpdateCksSpotCheck(parm);
            return ToResponse(result > 0, TIME_FORMAT_FULL);
        }

        /// <summary>
        /// 删除抽检记录
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [Log(Title = "产品抽检", BusinessType = BusinessType.DELETE)]
        [HttpDelete("{ids}")]
        [ActionPermissionFilter(Permission = "produce:cksspotcheck:remove")]
        public IActionResult DeleteCksSpotCheck(string ids) {
            if (string.IsNullOrWhiteSpace(ids)) {
                return ToResponse(ResultCode.PARAM_ERROR, "params error.");
            }
            long[] spotCheckIds = Tools.SpitLongArrary(ids);
            int result = _cksSpotCheckService.DeleteCksSpotCheckByIds(spotCheckIds);
            return ToResponse(result > 0, TIME_FORMAT_FULL);
        }


        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="formFile">使用IFromFile必须使用name属性否则获取不到文件</param>
        /// <returns></returns>
        [HttpPost("importData")]
        [Log(Title = "抽检导入", BusinessType = BusinessType.IMPORT, IsSaveRequestData = false, IsSaveResponseData = true)]
        [ActionPermissionFilter(Permission = "produce:cksspotcheck:import")]
        public IActionResult ImportData([FromForm(Name = "file")] IFormFile formFile) {
            List<CksSpotCheck> spotChecks = new();
            using (var stream = formFile.OpenReadStream()) {
                spotChecks = stream.Query<CksSpotCheck>(startCell: "A2").ToList();
            }
            return SUCCESS(_cksSpotCheckService.ImportSpotCheckData(spotChecks));
        }

        /// <summary>
        /// 抽检导入模板下载
        /// </summary>
        /// <returns></returns>
        [HttpGet("downloadExportTemplate")]
        [Log(Title = "抽检模板", BusinessType = BusinessType.EXPORT, IsSaveRequestData = true, IsSaveResponseData = false)]
        [AllowAnonymous]
        public IActionResult DownloadExportTemplate() {
            (string, string) result = DownloadImportTemplate("spotcheck");
            return ExportExcel(result.Item2, result.Item1);
        }

    }
}